/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
	CREATES:	${out_tables}/T19A_inverseprob.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/

	* Load data
	use "${dt_final}/panel_RR_indexes.dta", clear


	* Establish macros for regressions

/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
	
	* Outcomes
	*-----------------------------------------------------------
	local childoutcomes  "zDCCSPunt zptevir_irtscore2 zt_score zcbcl_t_e zcbcl_t_i zibatt_sspi zibatt_ssai zibatt_sssr zas_raw_score" 
	local behaviors "home_score se_score disc_score" 
	local beliefs "zpstyle1 zpstyle2 zpstyle3 zPSCS zPSSS_family zPSSS_friends zPSSS_others zPSI_Distress zCESD zpacotis"	
	local child "zptevir_irtscore2 zcog_index zsoc_index" 

	* Controls
	*-----------------------------------------------------------

	macro  def Xvar1 "i.age_year i.gender"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
	macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  

	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------

	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"
	

/*******************************************************************************
Table A19: Inverse Probability Weighting (z-index)
*******************************************************************************/
	
su nonatt t_score_old dCCSPunt_old i.inc_qtaut_old i.age_year_old female_old i.hh_tipo_att i.ch_bthorder_at pc_age_old  active_old full_time_old i.TIPO_old
	
* 1st Stage * EXCLUDE EDUCATION AND INCLUDE BASELINE LANGUAGE, BASELINE EF, AND EXCLUDING FEMALE HEADED (ONLY SIGNIFICANT PREDICTORS)
xtlogit nonatt t_score_old dCCSPunt_old i.inc_qtaut_old i.age_year_old female_old i.hh_tipo_att i.ch_bthorder_at pc_age_old  active_old full_time_old i.TIPO_old, fe i(CENTRO_SALUD_old)

predict p_nonatt

*replace p_nonatt=. if p_nonatt>0.09

gen w=.
replace w=1/p_nonatt if nonatt==1 
replace w=1/(1-p_nonatt) if nonatt==0
su w p_nonatt nonatt
replace w=. if w>50


local f "zptevir_irtscore2 zcog_index zsoc_index " 
foreach var of local f {
xi: reg `var' i.TIPO i.CENTRO_SALUD [pw=w]
estimates store `var'
test _ITIPO_2==_ITIPO_3
estadd scalar pb=r(p)				
qui sum `var' if e(sample) & TIPO==1
}

**Note for Margarita: this table needs to be corrected because the one in the paper does not use the age-standardized scores!
	esttab  zptevir_irtscore2  zcog_index zsoc_index  using "${out_tables}/appendix/T19A_inverseprob.csv", replace f ///	   
	keep(_ITIPO_2 _ITIPO_3) /*
	*/	cells("b(fmt(3)star)" "se(fmt(3)par)") /*
	*/ 	star(* 0.10 ** 0.05 *** 0.01) /*
	*/	stats(r2 pb N , fmt(2 3 0) /*
	*/ labels("R-squared" "P-value NEP-B=NEP-I" "Observations" )) legend	 /*
	*/ mtitles("Vocabulary Index" "Executive Function Index" "Socioemotional Index"  ) /*
	*/ varlabels(_ITIPO_2 "NEP-B" _ITIPO_3 "NEP-I")
	
	


